package com.yxjx.duoxue.my;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.yxjx.duoxue.BaseActionBarActivity;
import com.yxjx.duoxue.R;
import com.yxjx.duoxue.customview.MyProgressDialog;
import com.yxjx.duoxue.dao.Dao;
import com.yxjx.duoxue.datamodel.ResponseObject;
import com.yxjx.duoxue.network.MsgCode;
import com.yxjx.duoxue.network.UserLoginResponse;
import com.yxjx.duoxue.network.UserReg;
import com.yxjx.duoxue.user.UserLogin;
import com.yxjx.duoxue.utils.UIHelper;
import com.yxjx.duoxue.utils.Utils;

public class ModifyPasswordActivity extends BaseActionBarActivity {
	
	private static final int MSG_MODIFY_PWD_DONE = 1;
	
	private ProgressDialog dialog;
	private View rootView;
	private Context mContext;

	private EditText reNewPwdEditText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_modify_password);
		customizeActionBarTitle("修改密码");
		rootView = findViewById(R.id.root);
		mContext = rootView.getContext();
		reNewPwdEditText = (EditText) rootView.findViewById(R.id.reNewPwd);

		reNewPwdEditText.setOnEditorActionListener(new EditText.OnEditorActionListener() {

			@Override
			public boolean onEditorAction(TextView v, int actionId,
					KeyEvent event) {
				Utils.logd("onEditorAction actionId = " + actionId);
				if (actionId == EditorInfo.IME_ACTION_GO) {
					changePwd(rootView);
					return true;
				}
				return false;
			}
		});
	}
	
	public void changePwd(View view) {
		
		oldPwd = UIHelper.getEditText(rootView, R.id.oldPwd);
		newPwd = UIHelper.getEditText(rootView, R.id.newPwd);
		reNewPwd = UIHelper.getEditText(rootView, R.id.reNewPwd);
		
		if (UIHelper.isEmpty(oldPwd)) {
			UIHelper.showTips(view, "当前密码不能为空");
			return;
		}
		
		if (UIHelper.isEmpty(newPwd)) {
			UIHelper.showTips(view, "新密码不能为空");
			return;
		}
		
		if (UIHelper.isEmpty(reNewPwd)) {
			UIHelper.showTips(view, "确认密码不能为空");
			return;
		}
		
		dialog = MyProgressDialog.show(this, "", "正在修改密码");
		Thread thread = new Thread(new Runnable() {
			@Override
			public void run() {
				hideIME(rootView);
				ResponseObject rsp = UserReg.changepwd(mContext, oldPwd, newPwd, reNewPwd);
				if (rsp.isOk()) {
					// 更新userLoginRsp
					UserLoginResponse ulRsp = Dao.getUlRsp(mContext);
					ulRsp.setUserPassword(newPwd);
					Dao.setUlRsp(mContext, ulRsp);
					UserLogin.tryUserLogin(mContext);
				}
				mHandler.sendMessage(mHandler.obtainMessage(MSG_MODIFY_PWD_DONE, rsp));
			}
		});
		thread.start();
		
	}
	
	Handler mHandler = new Handler() {
		
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MSG_MODIFY_PWD_DONE:
				tryDismissDialog();
				ResponseObject rsp = (ResponseObject) msg.obj;
				if (rsp.isOk()) {
					UIHelper.showTips(rootView, "修改成功");
					finish();
				} else {
					UIHelper.showTips(rootView, MsgCode.descFor(rsp));
				}
				break;
			default:
				break;
			}
		};
	};

	private String oldPwd;
	private String newPwd;
	private String reNewPwd;
	
	public void tryDismissDialog() {
		if (dialog != null) {
			dialog.dismiss();
			dialog = null;
		}
	}

}
